Skip to content

Conversation

@SamiSuleiman
Copy link

add a button to toggle ligatures

solves #186

add a button to toggle ligatures
@SamiSuleiman
Copy link
Author

SamiSuleiman commented Oct 31, 2025

@braver let me know if any changes are needed here <3

@braver
Copy link
Owner

braver commented Oct 31, 2025

I like it! It needed some adjustments for the Monaspace font, and I tweaked the look a bit:

diff --git a/index.css b/index.css
index 06be33b..6ae94cc 100644
--- a/index.css
+++ b/index.css
@@ -280,12 +280,28 @@ section.config {
     @media screen and (max-width: 56em) {
         margin-right: 4px;
     }
+
+    > * {
+        text-align: center;
+        padding: 0;
+        white-space: nowrap;
+
+        > * {
+           height: 100%;
+           box-sizing: border-box;
+           margin: 0;
+        }
+    }
+
+    button {
+        background: transparent;
+        border: none;
+        path {
+            fill: var(--medium-grey);
+        }
+    }
 }
-section.config > * {
-    text-align: center;
-    padding: 0;
-    white-space: nowrap;
-}
+
 section.select-list {
     flex: 1;
     overflow: auto;
@@ -600,10 +616,14 @@ label + .nav-button {
     background: transparent;
     box-shadow: none !important;
     text-rendering: optimizeLegibility;
+    font-variant-ligatures: none;
 
     /* https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors#attrvalue_3 */
-    &[data-font|="monaspace"] {
-        font-feature-settings: 'calt', 'ss01', 'ss02', 'ss03', 'ss04', 'ss05', 'ss06', 'ss07', 'ss08', 'ss09', 'liga';
+    &.with-ligatures {
+        font-variant-ligatures: normal;
+        &[data-font|="monaspace"] {
+            font-feature-settings: 'calt', 'ss01', 'ss02', 'ss03', 'ss04', 'ss05', 'ss06', 'ss07', 'ss08', 'ss09', 'liga';
+        }
     }
 
     .cm-comment {
diff --git a/index.html b/index.html
index 5488abe..b9d719c 100755
--- a/index.html
+++ b/index.html
@@ -277,12 +277,7 @@
                     </select>
                 </p>
                     <p>
-                        <button id="liga-toggle" title="ligatures">
-                            <svg width="12" height="12" alt="yes" viewBox="0 0 12 12" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
-                                <g>
-                                    <path d="M7.87,5.4L5.97,4.52L5.97,3.003L10.5,5.414L10.5,6.591L5.97,8.997L5.97,7.485L7.87,6.605L1.5,6.605L1.5,5.4L7.87,5.4Z" style="fill-rule:nonzero;"/>
-                                </g>
-                            </svg>
+                        <button id="liga-toggle" class="selected" title="ligatures">
                             <svg width="12" height="12" alt="no" viewBox="0 0 12 12" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
                                 <g transform="matrix(0.829577,0,0,0.829577,-2.51769,4.72923)">
                                     <g transform="matrix(12,0,0,12,3.03411,5.29647)">
@@ -293,6 +288,11 @@
                                     </g>
                                 </g>
                             </svg>
+                            <svg class="selected" width="12" height="12" alt="yes" viewBox="0 0 12 12" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
+                                <g>
+                                    <path d="M7.87,5.4L5.97,4.52L5.97,3.003L10.5,5.414L10.5,6.591L5.97,8.997L5.97,7.485L7.87,6.605L1.5,6.605L1.5,5.4L7.87,5.4Z" style="fill-rule:nonzero;"/>
+                                </g>
+                            </svg>
                         </button>
                     </p>
                 </section>
diff --git a/modules/ligatures.js b/modules/ligatures.js
index 7b5f137..70996cd 100644
--- a/modules/ligatures.js
+++ b/modules/ligatures.js
@@ -48,8 +48,12 @@ export class Ligatures {
   }
 
   setLigatures (enabled) {
-    document.querySelectorAll('.CodeMirror pre').forEach(pre => {
-      pre.style.setProperty('font-variant-ligatures', enabled ? 'normal' : 'none')
+    document.querySelectorAll('.CodeMirror').forEach(cm => {
+      if (enabled) {
+        cm.classList.add('with-ligatures')
+      } else {
+        cm.classList.remove('with-ligatures')
+      }
     })
   }
 }

I'm not sure why disabling the ligatures this way doesn't work on the Maple font though?

@SamiSuleiman
Copy link
Author

I like it! It needed some adjustments for the Monaspace font, and I tweaked the look a bit:

diff --git a/index.css b/index.css
index 06be33b..6ae94cc 100644
--- a/index.css
+++ b/index.css
@@ -280,12 +280,28 @@ section.config {
     @media screen and (max-width: 56em) {
         margin-right: 4px;
     }
+
+    > * {
+        text-align: center;
+        padding: 0;
+        white-space: nowrap;
+
+        > * {
+           height: 100%;
+           box-sizing: border-box;
+           margin: 0;
+        }
+    }
+
+    button {
+        background: transparent;
+        border: none;
+        path {
+            fill: var(--medium-grey);
+        }
+    }
 }
-section.config > * {
-    text-align: center;
-    padding: 0;
-    white-space: nowrap;
-}
+
 section.select-list {
     flex: 1;
     overflow: auto;
@@ -600,10 +616,14 @@ label + .nav-button {
     background: transparent;
     box-shadow: none !important;
     text-rendering: optimizeLegibility;
+    font-variant-ligatures: none;
 
     /* https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors#attrvalue_3 */
-    &[data-font|="monaspace"] {
-        font-feature-settings: 'calt', 'ss01', 'ss02', 'ss03', 'ss04', 'ss05', 'ss06', 'ss07', 'ss08', 'ss09', 'liga';
+    &.with-ligatures {
+        font-variant-ligatures: normal;
+        &[data-font|="monaspace"] {
+            font-feature-settings: 'calt', 'ss01', 'ss02', 'ss03', 'ss04', 'ss05', 'ss06', 'ss07', 'ss08', 'ss09', 'liga';
+        }
     }
 
     .cm-comment {
diff --git a/index.html b/index.html
index 5488abe..b9d719c 100755
--- a/index.html
+++ b/index.html
@@ -277,12 +277,7 @@
                     </select>
                 </p>
                     <p>
-                        <button id="liga-toggle" title="ligatures">
-                            <svg width="12" height="12" alt="yes" viewBox="0 0 12 12" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
-                                <g>
-                                    <path d="M7.87,5.4L5.97,4.52L5.97,3.003L10.5,5.414L10.5,6.591L5.97,8.997L5.97,7.485L7.87,6.605L1.5,6.605L1.5,5.4L7.87,5.4Z" style="fill-rule:nonzero;"/>
-                                </g>
-                            </svg>
+                        <button id="liga-toggle" class="selected" title="ligatures">
                             <svg width="12" height="12" alt="no" viewBox="0 0 12 12" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
                                 <g transform="matrix(0.829577,0,0,0.829577,-2.51769,4.72923)">
                                     <g transform="matrix(12,0,0,12,3.03411,5.29647)">
@@ -293,6 +288,11 @@
                                     </g>
                                 </g>
                             </svg>
+                            <svg class="selected" width="12" height="12" alt="yes" viewBox="0 0 12 12" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
+                                <g>
+                                    <path d="M7.87,5.4L5.97,4.52L5.97,3.003L10.5,5.414L10.5,6.591L5.97,8.997L5.97,7.485L7.87,6.605L1.5,6.605L1.5,5.4L7.87,5.4Z" style="fill-rule:nonzero;"/>
+                                </g>
+                            </svg>
                         </button>
                     </p>
                 </section>
diff --git a/modules/ligatures.js b/modules/ligatures.js
index 7b5f137..70996cd 100644
--- a/modules/ligatures.js
+++ b/modules/ligatures.js
@@ -48,8 +48,12 @@ export class Ligatures {
   }
 
   setLigatures (enabled) {
-    document.querySelectorAll('.CodeMirror pre').forEach(pre => {
-      pre.style.setProperty('font-variant-ligatures', enabled ? 'normal' : 'none')
+    document.querySelectorAll('.CodeMirror').forEach(cm => {
+      if (enabled) {
+        cm.classList.add('with-ligatures')
+      } else {
+        cm.classList.remove('with-ligatures')
+      }
     })
   }
 }

I'm not sure why disabling the ligatures this way doesn't work on the Maple font though?

oh thats odd, it i didnt think about testing it with all fonts...
I'm not too familiar with fonts but maybe it's because the ligratures are baked in and are not "features" like with other fonts?

@braver
Copy link
Owner

braver commented Oct 31, 2025

There are 2 things going on: one is the browser and CSS setting options for the font (of which there are many, and I haven't fully figured out which CSS properties enable which options), the other is the font implementation (which options enable which ligatures). So yeah, it'd take me a minute to get this to work across all fonts 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants